﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UFSSDesktopApp.UFSSservicereference;
using UFSSClientClasses;
using System.Collections;

namespace UFSSDesktopApp
{
    public partial class FormFriends : Form
    {
        private UFSSservicereference.UFSSserviceSoapClient ussc;
        private UFSSservicereference.UserCreds ucreds;
        private ArrayList messlist = new ArrayList();
        private UFSSMessage[] message;
        private int messageCount;

        public FormFriends(UFSSservicereference.UFSSserviceSoapClient sc, UFSSservicereference.UserCreds uc)
        {
            InitializeComponent();
            ussc = sc;
            ucreds = uc;
        }

        private void FormFriends_Load(object sender, EventArgs e)
        {
            refresh();
            tabControl1.SetBounds(tabControl1.Location.X, tabControl1.Location.Y, 287, 238);

            //ControlPopulator.PopulateListControl(lstFriends, friends);
        }

        private void refresh()
        {
            //friends
            lstFriends.Items.Clear();
            string[] friends = ussc.getFriends(ucreds).ToArray<string>();
            int FriendCount = friends.Length;
            tpFriends.Text = "Friends (" + FriendCount + ")";
            lstFriends.Items.AddRange(friends);

            //request
            lbFriendRequests.Items.Clear();
            string[] requests = ussc.getRequestedFriends(ucreds).ToArray<string>();
            int requestCount = requests.Length;
            tpRequests.Text = "Requests (" + requestCount + ")";
            lbFriendRequests.Items.AddRange(requests);

            //messages
            lbMessage.Items.Clear();
            messlist.Clear();
            message = ussc.getMessages(ucreds);
            messageCount = message.Length;
            for (int i = 0; i < messageCount; i++)
                messlist.Add(message[i]);
            tpMessages.Text = "Messages (" + messageCount + ")";
            foreach (UFSSMessage mess in messlist)
            {
                lbMessage.Items.Add(mess.subject);
            }
        }

        private void btnDelFriends_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstFriends.SelectedItems.Count > 1)
                {
                    ArrayOfString friends = new ArrayOfString();
                    for (int i = 0; i < lstFriends.SelectedItems.Count; i++)
                        friends.Add(lstFriends.SelectedItems[i].ToString());

                    //How is an ArrayOfString not a string array @#$!
                    //>> it is now.
                    ussc.delMultiFriend(ucreds, friends);

                }
                else
                    ussc.delFriend(ucreds, lstFriends.SelectedItem.ToString());
                
                refresh();
            }
            catch
            {
                lblError.Text = "An error has occured.";
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            Form.ActiveForm.Hide();
            FormMain fm = new FormMain(ussc, ucreds);
            fm.Activate();
            fm.Show();
        }

        private void btnRequestFriend_Click(object sender, EventArgs e)
        {
            string friendname;
            friendname = txtRequestFriend.Text;
            if (ussc.addFriend(ucreds,friendname))
                lblError.Text = "Friend request sent!";
            else
                lblError.Text = "Error requesting friend";

            refresh();
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0 || tabControl1.SelectedIndex == 1)
            {
                tabControl1.SetBounds(tabControl1.Location.X, tabControl1.Location.Y, 287, 238);
            }
            else if (tabControl1.SelectedIndex == 2)
            {
                tabControl1.SetBounds(tabControl1.Location.X, tabControl1.Location.Y, 520, 238);
            }
            refresh();
        }
        
        private void lbMessage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (gbMessages.Text.Equals("Messages"))
            {
                message = ussc.getMessages(ucreds);
                int messageCount = message.Length;
                messlist.Add(message[lbMessage.SelectedIndex]);
                foreach (UFSSMessage mess in messlist)
                {
                    txtSender.Text = mess.senderName;
                    txtSubject.Text = mess.subject;
                    txtMessageCont.Text = mess.messageCont;
                    dtpDateTime.Value = mess.dateAdded;
                }
            }
            else
            {
                txtSender.Text = lbMessage.SelectedItem.ToString();
            }
        }

        private void btnDelMessage_Click(object sender, EventArgs e)
        {
            if (btnDelMessage.Text.Equals("Delete"))
            {
                int temp = 0;
                foreach (UFSSMessage mess in messlist)
                {
                    if (lbMessage.SelectedIndex == temp)
                        if (ussc.delMessage(ucreds, mess.messageID))
                            lblError.Text = "Message Deleted.";
                    temp++;
                }
                refresh();
            }
            else
            {
                if (!txtSubject.Text.Equals(""))
                {
                    if (ussc.sendMessage(ucreds, txtSender.Text, txtMessageCont.Text, txtSubject.Text))
                        lblError.Text = "Message Sent.";
                    else
                        lblError.Text = "Error Sending Message";
                }
                else
                {
                    if (ussc.sendMessage(ucreds, txtSender.Text, txtMessageCont.Text, "No Subject"))
                        lblError.Text = "Message Sent.";
                    else
                        lblError.Text = "Error Sending Message";
                }
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            lblError.Text = "";
            if (btnSend.Text.Equals("Send Message(s)"))
            {
                btnSend.Text = "Delete Message(s)";
                gbSender.Text = "Receiver";
                gbMessages.Text = "Friends";
                btnDelMessage.Text = "Send";
                txtSubject.Enabled = true;
                txtSender.Enabled = true;
                txtMessageCont.ReadOnly = false;
                txtMessageCont.Text = "";
                txtSender.Text = "";
                txtSubject.Text = "";
                dtpDateTime.Value = DateTime.Now;
                lbMessage.Items.Clear();
                string[] friends = ussc.getFriends(ucreds).ToArray<string>();
                int FriendCount = friends.Length;
                lbMessage.Items.AddRange(friends);
            }
            else
            {
                refresh();
                btnSend.Text = "Send Message(s)";
                gbSender.Text = "Sender";
                gbMessages.Text = "Messages";
                btnDelMessage.Text = "Delete";
                txtMessageCont.Text = "";
                txtSender.Text = "";
                txtSubject.Text = "";
                txtSubject.Enabled = false;
                txtSender.Enabled = false;
                txtMessageCont.ReadOnly = true;
                lbMessage.Enabled = true;
            }
        }

        private void btnAcceptFriend_Click(object sender, EventArgs e)
        {
            if (!ussc.acceptFriend(ucreds, lbFriendRequests.SelectedItem.ToString()))
                lblError.Text = "Error accepting friend.";
            refresh();
        }

        private void btnReject_Click(object sender, EventArgs e)
        {
            if (!ussc.rejectFriend(ucreds, lbFriendRequests.SelectedItem.ToString()))
                lblError.Text = "Error rejecting friend.";
            refresh();
        }

        private void tpFriends_Click(object sender, EventArgs e)
        {

        }
    }
}
